#coding: utf-8

# Create your views here.
from django.contrib.auth.decorators import login_required
from django.shortcuts import render_to_response
from django.template import RequestContext
#from django.http import HttpResponseRedirect
#from django.core.urlresolvers import reverse
#from forms import FractionsForm
from models import *
from django.contrib.auth.models import User

from google.appengine.ext import webapp
from django.contrib import messages
import copy
from django.utils.translation import ugettext as _

from core import signals

from django.http    import HttpResponse

from google.appengine.api import mail

@login_required        
def listaDespesas(request):
    object_list = Despesas.objects.all()
    nrTotalDespesas = Despesas.objects.count()
    valorTotalDespesas = 0
    for despesa in object_list:
        valorTotalDespesas = valorTotalDespesas + despesa.custo
    return render_to_response('core/despesas_list.html',
            locals(), context_instance=RequestContext(request)
    )

@login_required     
def listaReceitas(request):
    object_list = Receitas.objects.all().order_by("nrFactura")
    receitasRecebidas = Receitas.objects.all().filter(situacao=True)
    receitasPorReceber = Receitas.objects.all().filter(situacao=False)
    nrTotalReceitas = Receitas.objects.count()
    valorTotalReceitas = 0
    for receitas in receitasRecebidas:
        valorTotalReceitas = valorTotalReceitas + receitas.valor
    valorTotalPorReceber = 0
    for porReceber in receitasPorReceber:
        valorTotalPorReceber = valorTotalPorReceber + porReceber.valor
    valorTotalExercicio = 0
    for receita in object_list:
        valorTotalExercicio = valorTotalExercicio + receita.valor
    return render_to_response('core/receitas_list.html',
            locals(), context_instance=RequestContext(request)
    )


@login_required            
def saldo(request):
    receitasRecebidas = Receitas.objects.all().filter(situacao=True)
    TotalReceitas = Receitas.objects.all()
    #receitasRecebidas = Receitas.objects.all()
    despesas = Despesas.objects.all()
    TotalReceitasRecebidas = 0
    for receitaRecebida in receitasRecebidas:
        TotalReceitasRecebidas = TotalReceitasRecebidas + receitaRecebida.valor
    TotalDespesas = 0
    for despesa in despesas:
        TotalDespesas = TotalDespesas + despesa.custo
    ValorTotalReceitas = 0
    for receita in TotalReceitas:
        ValorTotalReceitas = ValorTotalReceitas + receita.valor
    SaldoActual = TotalReceitasRecebidas - TotalDespesas
    return render_to_response('core/saldo.html',
            locals(), context_instance=RequestContext(request)
    )
    



@login_required
def mainPage(request):
    """
    Personal data of the user profile
    """
    UserProfiles = Condominos.objects.all().filter(user=request.user)
    profile, created = Condominos.objects.get_or_create(user=request.user)

    if request.method == "POST":
        old_profile = copy.copy(profile)
        form = ProfileForm(request.POST, instance=profile)
        if form.is_valid():
            form.save()
            messages.info(request, _("Your profile was updated with success."), fail_silently=True)
            signal_responses = signals.post_signal.send(sender=mainPage, request=request, form=form, extra={'old_profile':old_profile})
            last_reponse = signals.last_response(signal_responses)
            if last_reponse:
                return last_response
    else:
        form = ProfileForm(instance=profile)
    #show_last_login(request)
    template = "home.html"
    data = { 'section': 'mainPage',
             'form': form, }
    signals.context_signal.send(sender=mainPage, request=request, context=data)
    return render_to_response(template, locals(), context_instance=RequestContext(request))
    



@login_required
def meusPagamentos(request):
    try:
        a = Condominos.objects.all().filter(user=request.user).get()
        b = Fraccoes.objects.all().filter(proprietario=a).get()
        meusPagamentos = Receitas.objects.all().filter(fraccao=b).order_by('nrFactura')
    except:
        messages.info(request, _("No records associated."), fail_silently=True)
    
    return render_to_response('core/meusPagamentos.html',
            locals(), context_instance=RequestContext(request)
    )












